$(function() {

	var historyPage = {
		createNew: function() {
			var history = {};
			history.init = function() {
				this.initData();
				this.bindEvent();
			};
			history.initData = function() {
				history.invokeHistory({});
			};
			history.bindEvent = function() {
				//查询事件
				history.queryHistory();
				//点击事件
				history.queryDetail();
				//分页
				history.pageAction();
			};
			history.queryHistory = function() {
				$("#searchHistory").on("click", function() {
					$("#pageNO").html(1);
					history.invokeHistory();
				});
			};

			history.invokeHistory = function() {
				var beginDate = $("#begin_date").val();
				var endData = $("#end_date").val();
				var report_type = $("#report_type").val();
				//				if ((beginDate == "") && (endData == "") && (report_type == "")) {
				//					$.toast("请选择查询条件");
				//					return;
				//				}

				//				if (beginDate || endData) {
				//					if (beginDate == "") {
				//						$.toast("请选择开始时间");
				//					}
				//					if (endData == "") {
				//						$.toast("请选择结束时间");
				//						return;
				//					}
				//				}
				var date1 = Date.parse(beginDate);
				var date2 = Date.parse(endData);
				if (beginDate && endData && (date1 > date2)) {
					$.toast("开始时间大于结束时间");
					return;
				}

				var requestData = {};
				if (report_type) {
					requestData.report_type = report_type;
				}
				if (beginDate) {
					requestData.begin_date = beginDate;
				}
				if (endData) {
					requestData.end_date = endData;
				}
				requestData.page_no = $("#pageNO").html();
				requestData.u_id = getLocalStorage(STORATE_MANAGER.user).u_id;
				$.ajax({
					type: "GET",
					url: CONFIGER_MANAGER.DOMAIN + "ajax_his_report_list",
					async: true,
					dataType: "JSON",
					data: requestData,
					beforeSend: function() {
						$.showWaiting()
					},
				}).done(function(data) {
					if (false == data.success) {
						$.toast(data.error_msg);
						return;
					}
					history.paintPage(data);
				}).fail(function() {

				}).always(function() {
					$.closeWaiting();
				});
			};
			history.paintPage = function(data) {
				var hisreport = data.data.his_report;				
				if(hisreport.length == 0){
					$.toast('没有数据')
					$("#historydiv").html('');
					return;
				}
				var i = 0;
				var html = [];
				while (hisreport[i]) {
					var report = hisreport[i];
					var apinos = hisreport[i].api_nos;
					var j = 0;
					html.push('<div class="yizu xuanzhon">');
					html.push('<div class="gaizhang ' + ((hisreport[i].info_type == '1') ? 'grbg_gaizhang' : 'qybg_gaizhang') + '"></div>');
					html.push('<table width="940" border="0" cellspacing="0" cellpadding="0" class="lcxbgbg">');
					html.push('<tbody>');
					html.push('<tr>');
					html.push('<td width="23" class="beijinxiaoqiuqiu"><span class="u_tp ' + ((hisreport[i].info_type == '1') ? 'u179' : 'u171') + ' "></span></td>');
					html.push('<td width="110" rowspan="2" valign="top" class="riqi">' + report.date_created.substr(0, 10));
					html.push('<span>浏览</span>');
					html.push('<span class="grzxcx">');
					var tips = {
						trans_log_no: report.trans_log_no,
						info_type: report.info_type
					}
					html.push('<input name="checkDetail" type="button" tip=' + JSON.stringify(tips) + ' class="saixuananniu djckbg" value="点击查看报告">');
					html.push('</span>');
					html.push('</td>');
					html.push('<td width="817" rowspan="3" valign="top" class="nr">');
					html.push('<ul>');
					while (apinos[j]) {
						html.push('<li>' + apinos[j].api_name + '</li>');
						j++;
					}
					html.push('</ul>');
					html.push('</td>');
					html.push('</tr>');
					html.push('<tr>');
					html.push('<td>&nbsp;</td>');
					html.push('</tr>');
					html.push('<tr>');
					html.push('<td>&nbsp;</td>');
					html.push('<td>&nbsp;</td>');
					html.push('</tr>');
					html.push('</tbody>');
					html.push('</table>');
					html.push('</div>');
					i++;
				}
				$("#historydiv").html(html.join(''));
				$("#pageNO").html(data.data.page_no);
				$("#numPages").html(data.data.num_pages);
			};
			history.queryDetail = function() {
				$("#historydiv").on("click", "input[name=checkDetail]", function() {
					var tips = JSON.parse($(this).attr('tip'));
					redirect("result.html?trans_log_no=" + tips.trans_log_no + "&info_type=" + tips.info_type);
				});
			},
			history.pageAction = function() {
				$("input[name=pagenumber]").on("click", function() {
					var addOr = $(this).attr("id");
					var pageNO = parseInt( $("#pageNO").html() );
					var numPages = parseInt( $("#numPages").html() );
					if ("prev" == addOr) {
						pageNO = pageNO - 1;
					} else {
						pageNO = pageNO + 1;
					}
					if (pageNO < 1) {
						pageNO = 1;
						return;
					}
					if(pageNO > numPages){
						return;
					}
					$("#pageNO").html(pageNO);
					history.invokeHistory();
				});
			}
			return history;
		}
	};

	var thisPage = historyPage.createNew();
	thisPage.init();
});